package com.bear.newsset.dao;

import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import com.bear.newsset.dao.entity.ImageSqlEntity;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "IMAGE_SQL_ENTITY".
*/
public class ImageSqlEntityDao extends AbstractDao<ImageSqlEntity, Long> {

    public static final String TABLENAME = "IMAGE_SQL_ENTITY";

    /**
     * Properties of entity ImageSqlEntity.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property NewsId = new Property(1, String.class, "newsId", false, "NEWS_ID");
        public final static Property Url = new Property(2, String.class, "url", false, "URL");
        public final static Property CreateTime = new Property(3, Long.class, "createTime", false, "CREATE_TIME");
    }

    private Query<ImageSqlEntity> newsDataSqlEntity_ImageUrlListQuery;

    public ImageSqlEntityDao(DaoConfig config) {
        super(config);
    }
    
    public ImageSqlEntityDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"IMAGE_SQL_ENTITY\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"NEWS_ID\" TEXT," + // 1: newsId
                "\"URL\" TEXT," + // 2: url
                "\"CREATE_TIME\" INTEGER);"); // 3: createTime
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"IMAGE_SQL_ENTITY\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, ImageSqlEntity entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String newsId = entity.getNewsId();
        if (newsId != null) {
            stmt.bindString(2, newsId);
        }
 
        String url = entity.getUrl();
        if (url != null) {
            stmt.bindString(3, url);
        }
 
        Long createTime = entity.getCreateTime();
        if (createTime != null) {
            stmt.bindLong(4, createTime);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, ImageSqlEntity entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String newsId = entity.getNewsId();
        if (newsId != null) {
            stmt.bindString(2, newsId);
        }
 
        String url = entity.getUrl();
        if (url != null) {
            stmt.bindString(3, url);
        }
 
        Long createTime = entity.getCreateTime();
        if (createTime != null) {
            stmt.bindLong(4, createTime);
        }
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public ImageSqlEntity readEntity(Cursor cursor, int offset) {
        ImageSqlEntity entity = new ImageSqlEntity( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // newsId
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // url
            cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3) // createTime
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, ImageSqlEntity entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setNewsId(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setUrl(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setCreateTime(cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(ImageSqlEntity entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(ImageSqlEntity entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(ImageSqlEntity entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "imageUrlList" to-many relationship of NewsDataSqlEntity. */
    public List<ImageSqlEntity> _queryNewsDataSqlEntity_ImageUrlList(String newsId) {
        synchronized (this) {
            if (newsDataSqlEntity_ImageUrlListQuery == null) {
                QueryBuilder<ImageSqlEntity> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.NewsId.eq(null));
                newsDataSqlEntity_ImageUrlListQuery = queryBuilder.build();
            }
        }
        Query<ImageSqlEntity> query = newsDataSqlEntity_ImageUrlListQuery.forCurrentThread();
        query.setParameter(0, newsId);
        return query.list();
    }

}
